<?php

namespace App\Models;

use App\Services\OrderService;
use Illuminate\Database\Eloquent\Model;

class OrderCredit extends Model
{
    protected $table = "sp_order_credit";
    protected $primaryKey = 'id';
    public $timestamps = false;

    //查询征信信息
    protected function getCreditData($order_id)
    {
        $time = date('Y-m-d H:i:s',time());
        $data = $this
            ->where('order_id',$order_id)
            ->orderby('id','desc')
            // ->where('end_at','>',$time)
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //添加风控信息
    protected function setRiskData($order_id,$result,$cert_name='',$mobile='',$cert_no='')
    {
        $risk_data['order_id'] = $order_id;
        $risk_data['cert_name'] = $cert_name;
        $risk_data['mobile'] = $mobile;
        $risk_data['cert_no'] = $cert_no;
        $risk_data['result2'] = json_encode($result);
        $risk_data['requestId'] = $result['data']['orderId'];
        $risk_data['scoreNo'] = $result['data']['applyOrderId'];

        $risk_data['start_at'] = date('Y-m-d H:i:s',time());
        $risk_data['end_at'] =  date('Y-m-d H:i:s',time()+7*24*60*60);

        //判断数据库没有才添加
      $credit = $this->where('order_id',$order_id)->first();

      $credit = $credit ? $credit->toArray() : array();

      if(empty($credit)){
          $this->insert($risk_data);
       }else{
          $this->where('order_id',$order_id)->update([
              'result2'=>json_encode($result),
              'requestId'=>$result['data']['orderId'],
              'scoreNo'=>$result['data']['applyOrderId'],
          ]);
      }

        return true;
    }
    //编辑征信信息
    protected function editCreditData($order_id,$risk_data)
    {
        $this->where('order_id',$order_id)->update($risk_data);

        return true;
    }

}
